package net.gyapp.markim.core;

import com.easyder.wrapper.utils.LogUtils;
import com.xuhao.android.libsocket.sdk.ConnectionInfo;
import com.xuhao.android.libsocket.utils.ActivityStack;

import net.gyapp.markim.WrapperApplication;
import net.gyapp.markim.core.entity.ConversationEntity;
import net.gyapp.markim.core.entity.MessageEntity;
import net.gyapp.markim.core.reponse.ChatResponse;
import net.gyapp.markim.core.reponse.ImResponse;
import net.gyapp.markim.core.reponse.LoginResponse;
import net.gyapp.markim.core.reponse.OfflineMessageResponse;
import net.gyapp.markim.core.reponse.UserGroupResponse;
import net.gyapp.markim.jpush.CustomReceiver;
import net.gyapp.markim.module.basic.event.MessageChanged;
import net.gyapp.markim.module.basic.event.MessageIml;
import net.gyapp.markim.module.basic.event.ResponseChanged;
import net.gyapp.markim.module.basic.event.ResponseIml;

import org.greenrobot.eventbus.EventBus;
import org.jim.common.packets.Command;

import java.util.List;

/**
 * Author:  winds
 * Data:    2018/8/2
 * Version: 1.0
 * Desc:
 */


public class ImProcessor {


    /**
     * 处理收到的信息  除心跳
     *
     * @param info
     * @param response
     */
    public static void processResponseData(ConnectionInfo info, final ImResponse response) {
        if (response instanceof LoginResponse) {    //登录返回信息
            processUserGroupData((LoginResponse) response);
        } else if (response instanceof ChatResponse) { //正常聊天消息
            ChatResponse chatResponse = (ChatResponse) response;
            if (chatResponse.data != null) {
                if (chatResponse.data.cmd == Command.COMMAND_CHAT_RESP.getNumber()) {
                    //聊天消息响应
                    processMessageResponseData(chatResponse);
                    return;
                } else if (chatResponse.data.cmd == Command.COMMAND_CANCEL_MSG_REQ.getNumber()) {
                    //撤回聊天消息
                    processMessageRecallData(chatResponse);
                    return;
                }
            }
            processMessageData(chatResponse);
        } else if (response instanceof OfflineMessageResponse) { //离线消息
            processOfflineMessageData((OfflineMessageResponse) response);
        } else if (response instanceof UserGroupResponse) {
            processUserGroupData((UserGroupResponse) response);
        }
    }


    /**
     * 处理会话消息
     *
     * @param response
     */
    public static void processMessageData(ChatResponse response) {
        ImHelper.updateOrInsertMessage(response);
        if (!ImHelper.isSelf(response.data.from)) {
            ImManager.getInstance().playVibrate();
            ImManager.getInstance().playRingtone();

            if (ActivityStack.size() == 0) {
                LogUtils.info("--> Receiver 保活通知");
                CustomReceiver.onPauseMessageReceive(WrapperApplication.getInstance());
            }

            EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_CHAT_MESSAGE));
            EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_CONVERSATION_REFRESH));
        }
    }

    /**
     * 处理消息响应
     *
     * @param response
     */
    public static void processMessageResponseData(ChatResponse response) {
        ImHelper.updateOrInsertMessage(response);
    }

    /**
     * 处理撤回消息
     *
     * @param response
     */
    public static void processMessageRecallData(ChatResponse response) {
        boolean bool = ImHelper.updateRecallMessage(response);
        if (bool) {
            //更新
            ImHelper.updateOrInsertConversation(response);
            EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_MESSAGE_RECALL));
        }
    }

    public static void processSelfMessageRecallData(ConversationEntity conversationEntity, MessageEntity entity) {
        ImHelper.updateRecallSelfConversation(conversationEntity, entity);
        EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_MESSAGE_RECALL));
    }

    /**
     * 处理好友数据  默认处理
     *
     * @param response
     */
    public static void processUserGroupData(final LoginResponse response) {
        if (WrapperApplication.isLogin()) {
            ImHelper.updateOrInsertUser(WrapperApplication.getMember(), response);
            if (response.data == null) {
                ImManager.getInstance().getAllUser();
            } else {
                EventBus.getDefault().post(new ResponseChanged(ResponseIml.SIGN_PROCESS_LOGIN_RESPONSE_SUCCESS));
                EventBus.getDefault().post(new ResponseChanged(ResponseIml.SIGN_PROCESS_LOGIN_RESPONSE_SUCCESS_UPDATE_FRIEND));
            }
        }
        ImManager.getInstance().getOfflineMessage();
    }

    private static void processUserGroupData(UserGroupResponse response) {
        if (WrapperApplication.isLogin()) {
            ImHelper.updateOrInsertUser(response);
            EventBus.getDefault().post(new ResponseChanged(ResponseIml.SIGN_PROCESS_LOGIN_RESPONSE_SUCCESS));
            EventBus.getDefault().post(new ResponseChanged(ResponseIml.SIGN_PROCESS_LOGIN_RESPONSE_SUCCESS_UPDATE_FRIEND));
        }
    }

    /**
     * 处理离线消息
     *
     * @param response
     */
    public static void processOfflineMessageData(OfflineMessageResponse response) {
        ImHelper.insertMessage(response);
        EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_CHAT_MESSAGE));
        EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_CONVERSATION_REFRESH));
    }


    public static void updateOrInsertConversation(ConversationEntity entity) {
        ImHelper.updateOrInsertConversation(entity);
        EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_CONVERSATION_REFRESH));
    }

    /**
     * 标记消息为已读   此处需要通知会话列表刷新未读数目
     *
     * @param list
     */
    public static void setMessageIsRead(List<MessageEntity> list) {
        boolean b = ImHelper.setMessageIsRead(list);
        if (b) {
            EventBus.getDefault().post(new MessageChanged(MessageIml.SIGN_CONVERSATION_REFRESH));
        }
    }
}
